//
//  RwdHomeController.swift
//  Weibo_rwd
//
//  Created by 任文迪 on 16/7/9.
//  Copyright © 2016年 spaceX. All rights reserved.
//

import UIKit
import SVProgressHUD
private let CellReuseIdentifier = "CellReuseIdentifier"
class RwdHomeController: RwdCommonController {
    //添加系统的下拉刷新的控件
    private lazy var pullDownView:UIRefreshControl = {
        let pullDown = UIRefreshControl()
        pullDown.addTarget(self, action: "didRefreshing", forControlEvents: .ValueChanged)
        return pullDown
    }()
    
    @objc private func didRefreshing() {
        
        loadStatus()
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), { () -> Void in
            //  结束刷新
            self.pullDownView.endRefreshing()
        })
        
    }
    
    
    //添加上拉加载的控件
    private lazy var pullUpView : UIActivityIndicatorView = {
        let indicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        indicatorView.color = UIColor.blackColor()
        
        return indicatorView
        
    }()
    //设置模型
    private lazy var statusListViewModel: RwdStatusListModel = RwdStatusListModel()
    //重写viewdidload方法
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if isLogin {
            loadStatus()
            setUpTableView()
            
            
        }else{
            
            visitorView?.updateVisitorInfos(nil, imageName: nil)
            
        }
    }
    //网络请求
    private func loadStatus() {
        statusListViewModel.loadSomething(pullUpView.isAnimating()) { (isSuccess) -> () in
            //进来以后先停止动画
            self.stopRefresh()
            if isSuccess {
                
                self.tableView.reloadData()
            }
            
        }
        
        
    }
    
    //停止动画
    private func stopRefresh() {
        
        pullUpView.stopAnimating()
        pullDownView.endRefreshing()
    }
    
    //设置UI
    private func setUpTableView() {
        
        tableView.registerClass(RwdTableViewCell.self, forCellReuseIdentifier: CellReuseIdentifier)
        
        tableView.separatorStyle = .None
        //添加上拉刷新的footerview
        tableView.addSubview(pullUpView)
        
        tableView.tableFooterView = pullUpView
        //自适应大小
        pullUpView.sizeToFit()
        
        //系统自带的下拉刷新
        tableView.addSubview(pullDownView)
        
        tableView.rowHeight = UITableViewAutomaticDimension
        //  预估行高
        tableView.estimatedRowHeight = 200
        
        
    }
    
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //        print("111")
        //        print(statusListViewModel)
        return statusListViewModel.Status.count
        
    }
    
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(CellReuseIdentifier, forIndexPath: indexPath) as! RwdTableViewCell
        
        //        cell.textLabel?.text = self.Status![indexPath.row].text
        let statusModel = statusListViewModel.Status[indexPath.row]
        
        cell.statusViewModel = statusModel
        
        cell.selectionStyle = .None
        
        return cell
        
        
    }
    
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        
        if indexPath.row == statusListViewModel.Status.count-1 && !pullUpView.isAnimating()
        {
            pullUpView.startAnimating()
            print("请求数据啦~")
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), { () -> Void in
                self.loadStatus()
            })
            
            
            
        }
        
    }
    
    
}


